﻿using System;
using System.Collections.Generic;
using GeneralDFS.BusinessLogical;
using GeneralDFS.BusinessLogical.Chord;
using GeneralDFS.BusinessLogical.FileSystemLogical;
using GeneralDFS.BusinessLogical.IO;
using GeneralDFS.BusinessLogical.Nodes;
using GeneralDFS.DataEntity.Enum;
using GeneralDFS.DataEntity.IO;
using GeneralDFS.DataEntity.Nodes;
using GeneralDFS.DataEntity.Config;
using GeneralDFS.Common;

namespace GeneralDFS.BusinessLogical.RemoteObject
{
     [Serializable]
    public partial class NameNodeRemoteObject : FileSystemApi
    {
        public FileSystem FileSystem;
        public DataNodeCenter DataNodeCenter;

        private readonly NameNode _nameNode;

        public NameNodeRemoteObject(NameNode nameNode)
        {
            _nameNode = nameNode;
            FileSystem = nameNode.FileSystem;
            Init(FileSystem);
            DataNodeCenter = nameNode.DataNodeCenter;
        }

        public NameNodeState GetNameNodeState()
        {
            return _nameNode.LocalNode.GetNodeState();
        }

        #region DataNode
        /// <summary>
        /// 注册一个DataNode
        /// </summary>
        /// <param name="newNodeInfo"></param>
        /// <returns></returns>
        public Boolean RegisterDataNode(DataNodeInfoWithBlocks newNodeInfo)
        {
            return DataNodeCenter.RegisterDataNode(newNodeInfo);
        }

        public Boolean UpdateHeartBeat(String name, int rpcPort, int tcpPort, String ip, long freeSpace, long userdSpace)
        {
            return DataNodeCenter.UpdateHeartBeat(name, rpcPort, tcpPort, ip, freeSpace, userdSpace);
        }

        /// <summary>
        /// 节点上传Block列表
        /// </summary>
        /// <param name="dataNodeID"></param>s
        /// <param name="fileBlockMetaDatas"></param>
        public void BlockReport(DataNodeID dataNodeID, List<FileBlockMetaData> fileBlockMetaDatas)
        {
            _nameNode.BlockManager.ProcessBlockReport(dataNodeID, fileBlockMetaDatas);
        }

        public List<DataNodeInfoWithBlocks> GetDataNodeInfoList()
        {
            return DataNodeCenter.GetDataNodeInfoList();
        }

        public SearchResultState CheckFIleBlockContainsDataNode(String filePath, FileBlockMetaData metaData,
                                                                      DataNodeID dataNode)
        {
            return FileBlock.CheckFileBlockContainsDataNode(_nameNode, filePath, metaData, dataNode);
        }

        public FileBlockInfo GetFileBlock(string fileNodePath, Guid guid)
        {
            return FileBlock.GetFileBlock(FileSystem, fileNodePath, guid);
        }

        public string GetCommand(string name)
        {
            return DataNodeCenter.GetNodeCommandByServerName(name);
        }

        #endregion

        public void SaveFsImage()
        {
            FileSystem.SaveFsImage();
        }

        public DataNodeID GetLocalDataNodeID()
        {
            return DataNodeCenter.GetNodeID();
        }

        /// <summary>
        /// 获取主节点的随机一个DataNodeID
        /// </summary>
        /// <returns></returns>
        public DataNodeID GetDataNodeID()
        {
            return _nameNode.MasterNameNodeRemoteObject.GetLocalDataNodeID();
        }

        /// <summary>
        /// 获取节点配置
        /// </summary>
        /// <returns></returns>
        public NameNodeConfig GetNameNodeConfig()
        {
            return _nameNode.GetNodeConfig();
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }

         public void Shutdown()
         {
             GeneralDFS.BusinessLogical.Shutdown.PowerOff();
         }

         public void Reboot()
         {
             GeneralDFS.BusinessLogical.Shutdown.Reboot();
         }

         public void LogoOff()
         {
             GeneralDFS.BusinessLogical.Shutdown.LogoOff();
         }
    }
}